package itsm.isperp.module.controller.customer;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataResponse;
import itsm.isperp.framework.utils.HtmlUtil;
import itsm.isperp.framework.web.paging.RenderPager;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.solr.SolrKngKnowledgeDTO;
import itsm.isperp.module.entity.app.AppUploadFile;
import itsm.isperp.module.service.app.AppUploadFileService;
import itsm.isperp.module.service.knowledge.KngKnowledgeCategoryService;
import itsm.isperp.module.service.knowledge.KngKnowledgeCommentService;
import itsm.isperp.module.service.knowledge.KngKnowledgeItemService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2014-8-6
 * @since 1.0
 * 
 */
@Controller
@RequestMapping("customer/help/knowledge")
public class CusHelpKnowledgeController {

	@Autowired
	KngKnowledgeCategoryService kngKnowledgeCategoryService;

	@Autowired
	KngKnowledgeItemService kngKnowledgeItemService;

	@Autowired
	protected KngKnowledgeCommentService kngKnowledgeCommentService;

	@RequestMapping(value = { "" })
	public ModelAndView knowledge(HttpServletRequest req) {

		ModelAndView mv = new ModelAndView("customer/help/knowledge/knowledge");
		mv.addObject("warmTop10Entities",
				kngKnowledgeItemService.getTop10ItemPublic());
		mv.addObject("newTop10Entities",
				kngKnowledgeItemService.getNewTop10Item(10," access_right='public'"));
		mv.addObject("default3", "default");
		return mv;
	}

	@RequestMapping("searchResult")
	public ModelAndView knowledgeSearchResult(HttpServletRequest req) {
		KngKnowledgeItemService kngKnowledgeItemService = ContextHolder
				.getSpringBean("kngKnowledgeItemService");
		ModelAndView mv = new ModelAndView(
				"customer/help/knowledge/searchResult");
		String keyword = req.getParameter("keyword");
		String pageIndex = req.getParameter("page");
		String categoryId = req.getParameter("categoryId");

		mv.addObject("categoryId", categoryId);
		mv.addObject("keyword", keyword);

		int currentPage = 1;
		if (StringUtils.isNotEmpty(pageIndex)) {
			currentPage = Integer.parseInt(pageIndex);
		}

		Pageable pg = null;
		if (StringUtils.isEmpty(keyword)) {
			pg = new PageRequest(currentPage, 5);

			JqGridRequest request = new JqGridRequest(pg);

			if (StringUtils.isNotEmpty(categoryId)) {
				request.addFilterRule("treenode_path", JqGridFilterRuleOp.cn,
						categoryId);
			}
			request.addFilterRule("access_right", JqGridFilterRuleOp.eq,
					"public");
			Page<SolrKngKnowledgeDTO> r = kngKnowledgeItemService
					.findAllPublishedDetailByRequest(request);

			for (SolrKngKnowledgeDTO d : r) {
				d.setContents(HtmlUtil.delHTMLTag(d.getContents()));
			}

			mv.addObject(
					"pager",
					RenderPager.renderHtml(req, currentPage, 5,
							(int) r.getTotalElements()));

			mv.addObject("result", r);
			return mv;
		}
		Criteria[] cs = null;
		pg = new PageRequest(currentPage - 1, 5);
		if (StringUtils.isNotEmpty(categoryId)) {
			cs = new Criteria[1];
			cs[0] = new Criteria("categoryId").is(categoryId);
		}

		Page<SolrKngKnowledgeDTO> page = kngKnowledgeItemService
				.searchByKeyword(keyword, pg, cs,"public");

		mv.addObject("pager", RenderPager.renderHtml(req, currentPage,
				pg.getPageSize(), (int) page.getTotalElements()));
		mv.addObject("result", page);
		return mv;

	}

	@RequestMapping("knowledgeInfo")
	public ModelAndView knowledgeInfo(String id, HttpServletRequest req) {

		ModelAndView mv = new ModelAndView(
				"customer/help/knowledge/knowledgeInfo");
		String knowledgeId = req.getParameter("id");

		Map<String, Object> model = kngKnowledgeItemService
				.findOneDetailById(knowledgeId);
		mv.addObject("model", model);

		List<Map<String, Object>> comments = kngKnowledgeCommentService
				.findAllDetailByKnowledgeId(knowledgeId);
		mv.addObject("comments", comments);
		mv.addObject("commentCount", comments == null ? 0 : comments.size());

		mv.addObject("default3", "default");
		// 阅读数增加1
		kngKnowledgeItemService.increaseReadTimes(knowledgeId);

		AppUploadFileService appUploadFileService = ContextHolder
				.getSpringBean("appUploadFileService");

		DataResponse<AppUploadFile> uploadFiles = appUploadFileService
				.getBySourceId(knowledgeId);
		mv.addObject("uploadFiles", uploadFiles);
		return mv;

	}

	@RequestMapping("getTree")
	@ResponseBody
	public List<? extends ITreeNode> knowledgeGetTree(HttpServletRequest req) {
		return kngKnowledgeCategoryService.getTree("");
	}

}
